#include "Blocksort.cpp"
#include <assert.h>

void testConArchivo(bool debug=false){
	uInt blockSize = 500;
	BlocksortCompress bs( "input.txt" , blockSize);
	bs.setOutputFilename( "binary.dat" );

	bs.debug = debug;

	string L;
	uInt  I;
	bs.runNextBlock( &L, &I );

	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}	
	
	printf("'testConArchivo' OK(?)\n");
}

void testConArchivo2(bool debug=true){
	uInt blockSize = 100;
	BlocksortCompress bs( "input2.txt" , blockSize);
	bs.setOutputFilename( "binary.dat" );
	
	bs.debug = debug;

	string L;
	uInt  I;
	
	bs.runNextBlock( &L, &I );
	
	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}	
	
	bs.runNextBlock( &L, &I );

	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}
	
	bs.runNextBlock( &L, &I );

	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}
	
	
	bs.runNextBlock( &L, &I );

	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}
	
	printf("'testConArchivo2' OK(?)\n");
}

void testConArchivo3(bool debug=true){
	uInt blockSize = 24+7+5;
	BlocksortCompress bs( "input2.txt" , blockSize);
	bs.setOutputFilename( "binary.dat" );
	
	//bs.debug = debug;
	
	string L;
	uInt  I;
	
	while( ERROR_EOF != bs.runNextBlock( &L, &I ) ){

		if(debug){
			printf( "I: %d \n" , I );
			printf( "L: '%s'\n\n" , L.c_str() );
		}
	}
	
	
	printf("'testConArchivo3' OK(?)\n");
}

void testConString(bool debug=false){
	BlocksortCompress bs;

	bs.debug = debug;

	string L;
	uInt  I;
	
	
	bs.runBlock( "TARJETA" , &L, &I );
	
	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}
	
	assert( I == 5 );
	assert( L == "TTJRAAE" );
	
	printf("'testConString' OK.\n");
}

void testConString2(bool debug=false){
	BlocksortCompress bs;

	bs.debug = debug;

	string L;
	uInt  I;


	bs.runBlock( "PAPETAPE" , &L, &I );

	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}

	assert( I == 4 );
	assert( L == "TPPPEAAE" );
	
	
	
	
	bs.runBlock( "TARJETA" , &L, &I );
	
	if(debug){
		printf( "I: %d \n" , I );
		printf( "L: '%s'\n\n" , L.c_str() );
	}
	
	assert( I == 5 );
	assert( L == "TTJRAAE" );

	printf("'testConString2' OK.\n");
}

void testComprimir(bool debug=false){
	uInt blockSize = 1024*900;
	BlocksortCompress bs( "input2.txt" , blockSize);
	bs.setOutputFilename( "binary.dat" );
	bs.debug = debug;
	bs.run();
	printf("'testComprimir' OK(?).\n");
}

void testDescomprimir(bool debug=false){
	BlocksortDecompress bs("binary.dat");
	bs.setOutputFilename( "BS.out" );
	bs.debug = debug;
	bs.run();
	
	printf("'testDescomprimir' OK(?).\n");
}

void testDescomprimir2(bool debug=false){

	string texto;
	
	BlocksortDecompress::runBlock( "TPPPEAAE", 4 , 8 , &texto );
	assert( texto == "PAPETAPE" );
	
	
	BlocksortDecompress::runBlock( "TTJRAAE", 5 , 7 , &texto );
	assert( texto == "TARJETA" );
	
	
	BlocksortDecompress::runBlock( "LLHHAABB", 3 , 8 , &texto );
	assert( texto == "BLAHBLAH" );

	printf("'testDescomprimir' OK.\n");
	
}


void testDescomprimir3(bool debug=false){
	
	BlocksortDecompress bs("binary.dat");
	
	string texto;
	
	while( ERROR_EOF != bs.runNextBlock( &texto ) ){
		if(debug) cout << "'" << texto << "'" << endl;
	}
	if(debug) cout << "'" << texto << "'" << endl;

	printf("'testDescomprimir2' OK.\n");
	
}



int main(int argc , char* argv[]) {
		
	bool debug = false;
	
	if( argc == 2 ){
		debug = true;
	}
	
	//testConArchivo(debug);
	//testConArchivo2(debug); 
	//testConArchivo3(debug); 
	testComprimir(debug);
	
	//testConString(debug);
	//testConString2(debug);
	//testDescomprimir2(debug);
	//testDescomprimir3(debug);
	testDescomprimir(debug);
	
	return 0;
}
